using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NiSis.Data;
using NiSis.Entities;

namespace NiSis
{
    /// <summary>
    /// Gestisce l'operazione di deconsolidamento
    /// </summary>
    /// 
    public partial class Deconsolidamento : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MeseAnnoPicker1.SelectCommandVisible = false;
                MeseAnnoPicker1.DateLabel = "Seleziona il mese da deconsolidare";
                LoadGrid();
            }

        }
        protected void GrdConsolidamento_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            /// Todo: stored procedure per cambiare stato, secondo il bottone settare lo stato. La sp deve aggiornare la tabella
            /// poolstate
            /// 
            if (e.CommandSource.GetType().Name != "ImageButton")
                return;

            ImageButton btn = (ImageButton)e.CommandSource;
            int idBatch;
            int.TryParse(btn.Attributes["idBatch"], out idBatch);


           // DataRepository.VConsolidaUtenzeProvider.ChangeBatchState(idBatch, (int)SshStatoRelazioniList.Ipotizzato);
            Response.Redirect(
                string.Format("DeconsolidatedRelationShips.aspx?idbatch={0}&stato={1}",
                idBatch,
                btn.Attributes["stato"]));
        }

        protected void LoadGrid(int pageIndex)
        {
            if (!MeseAnnoPicker1.HasSelectedItem())
                return;

             VList<VBatchStatoCatene> tpSt = DataRepository.VBatchStatoCateneProvider.Get(
                string.Format("cd_storico_mensile_rete='{0}' and id_stato in ({1},{2}) ",
                MeseAnnoPicker1.DateValue,
                (int)SshStatoRelazioniList.Consolidato,
                (int)SshStatoRelazioniList.Confermato)
                , "DsShipper, cd_pool,DsSocieta");

            GrdDeConsolidamento.DataSource = tpSt;
            if (pageIndex!=-1)
                GrdDeConsolidamento.PageIndex = pageIndex;

            GrdDeConsolidamento.DataBind();
            
        }
        protected void LoadGrid()
        {
            LoadGrid(-1);
        }
        protected void cmdSelectShipper_Click(object sender, ImageClickEventArgs e)
        {
            LoadGrid();
            
        }

        protected void GrdDeconsolidamento_DataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                VBatchStatoCatene batch = (VBatchStatoCatene)e.Row.DataItem;

                ImageButton btn = (ImageButton)e.Row.FindControl("btnDeconsolida");
                btn.Attributes.Add("IdBatch", batch.IdBatch.ToString());
                btn.Attributes.Add("RowIndex", e.Row.RowIndex.ToString());

                Image img = (Image)e.Row.FindControl("imgState");
                img.ImageUrl = batch.IdStato == 3 ? "~/img/BlueBall.png" : "~/img/GreenBall.png";
                img.ToolTip = batch.IdStato == 3 ? "Consolidato" : "Verificato";
                btn.Attributes.Add("Stato", batch.IdStato.ToString()); 
            }
        }
        protected void gGrdDeConsolidamento_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            LoadGrid(e.NewPageIndex);

        }
        



    }
}
